//
//  MyPostSectionHeaderView.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/14.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "MyPostSectionHeaderView.h"

@implementation MyPostSectionHeaderView

+(CGFloat)calculateHWithModel:(CommonPostModel *)model {
    
    CGFloat topPading = 0;//距离顶部的距离
    
    CGFloat xiajiaHeight = 30;//用户信息高度
    //状态（1：待审核，2：审核拒绝，3：审核通过-上架，4：下架）
    switch (model.status) {
        case 1:{
            xiajiaHeight = CGFLOAT_MIN;
        } break;
        case 2:{
            xiajiaHeight = CGFLOAT_MIN;
        } break;
        case 3:{
            xiajiaHeight = CGFLOAT_MIN;
        } break;
        case 4:{
            xiajiaHeight = 30;
        } break;
        default:
            break;
    }
    
    CGFloat delHeight = 30;//

    CGFloat labHeight = model.textHeight;//文字高度可计算
    CGFloat imgContent = model.imageHeight;//计算这个来控制宽度
    
    CGFloat bottomHeight = 30 + 15;//底部交互框加距离
    return topPading + xiajiaHeight + delHeight + labHeight + imgContent + bottomHeight;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.xiajiaBtn pp_clipCorners:UIRectCornerAllCorners radius:self.xiajiaBtn.frame.size.height/2 border:1 color:UIColorHex(0xFF7200)];
    });
    
}


- (void)fillWithModel:(CommonPostModel *)model {
    
    switch (model.sysMediaList.count) {
        case 0:{

            self.contentImg1.hidden = YES;
            self.contentImg2.hidden = YES;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
        case 1:{
            
            [self simpleImageLayout];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = YES;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
            
        } break;
        case 2:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = NO;
            self.contentImg3.hidden = YES;
            self.contentImg3View.hidden = YES;
        } break;
        case 3:{
            
            [self imagesLayOut];
            self.contentImg1.hidden = NO;
            self.contentImg2.hidden = NO;
            self.contentImg3.hidden = NO;
            self.contentImg3View.hidden = NO;
        } break;
        default:
            break;
    }
    

    if (model.sysMediaList.count > 0) {
        [self.contentImg1 sd_setImageWithURL:[NSURL URLWithString:model.sysMediaList[0].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    
    if (model.sysMediaList.count > 1) {
        [self.contentImg2 sd_setImageWithURL:[NSURL URLWithString:model.sysMediaList[1].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    
    if (model.sysMediaList.count > 2) {
        [self.contentImg3 sd_setImageWithURL:[NSURL URLWithString:model.sysMediaList[2].url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    
    self.postContentHeight.constant = model.textHeight;
    self.imageContentHeight.constant = model.imageHeight;

    if (model.sysMediaList.count>3) {
        self.contentImg3View.hidden = NO;
        self.contentImg3Lab.text = [NSString stringWithFormat:@"+%u",model.sysMediaList.count-3];
    } else {
        self.contentImg3View.hidden = YES;
        self.contentImg3Lab.text = @"";
    }
    
    if (model.status == 4) {
        self.xiajiaHeight.constant = 30;
    } else {
        self.xiajiaHeight.constant = CGFLOAT_MIN;
    }
    
    [self layoutIfNeeded];
        
    self.timeLab.text = [NSString stringWithFormat:@"发布于：%@",model.createTime];
    self.contentLab.text = model.content;

    
//状态（1：待审核，2：审核拒绝，3：审核通过-上架，4：下架）
//    @property (nonatomic, copy) NSString *status;
    
    switch (model.status) {
        case 1:{
            [self.xiajiaBtn setTitle:@" 待审核" forState:UIControlStateNormal];
        } break;
        case 2:{
            [self.xiajiaBtn setTitle:@" 审核拒绝" forState:UIControlStateNormal];
        } break;
        case 3:{
            [self.xiajiaBtn setTitle:@" 上架" forState:UIControlStateNormal];
        } break;
        case 4:{
            [self.xiajiaBtn setTitle:[NSString stringWithFormat:@" 强制下架：%@",model.reason] forState:UIControlStateNormal];
        } break;
        default:
            break;
    }
    

    [self.tagBtn setTitle:[NSString stringWithFormat:@" %@",model.themeTitle] forState:UIControlStateNormal];
    
    self.evaThumbLab.text = [NSString stringWithFormat:@"评论%ld·赞%ld",model.replyCount,model.likes];
}

- (IBAction)delAction:(id)sender {
    if (self.MYPostSectionHeaderViewBlock) {
        self.MYPostSectionHeaderViewBlock(MYPostSectionHeaderActionType_del, -1);
    }
}


//单图的约束
- (void)simpleImageLayout {
    [NSLayoutConstraint deactivateConstraints:@[self.contentImg1Scale]];
    self.contentImg1Scale = [NSLayoutConstraint constraintWithItem:self.contentImg1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentImg1 attribute:NSLayoutAttributeHeight multiplier:1.4583 constant:0];
    [NSLayoutConstraint activateConstraints:@[self.contentImg1Scale]];

}

//多图的约束
- (void)imagesLayOut {
    
    [NSLayoutConstraint deactivateConstraints:@[self.contentImg1Scale]];
    self.contentImg1Scale = [NSLayoutConstraint constraintWithItem:self.contentImg1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentImg1 attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    [NSLayoutConstraint activateConstraints:@[self.contentImg1Scale]];
}


@end
